<template>
  <div class="style-page">
    <div class="head-page-title">{{ showComponentData.title }}</div>
    <div class="line"></div>

    <div class="component-style">
      <div class="header" @click="contentCollapsed = !contentCollapsed">
        <p class="title">内容样式</p>
        <el-icon v-if="!contentCollapsed"><ArrowDown /></el-icon>
        <el-icon v-else><ArrowRight /></el-icon>
      </div>

      <div v-if="!contentCollapsed">
        <el-form :model="form" label-width="auto">
          <el-form-item label="图片形状：">
            <el-radio-group v-model="form.borderRadius">
              <el-radio label="正常" :value="0" />
              <el-radio label="圆角" :value="12" />
              <el-radio label="圆形" :value="100" />
            </el-radio-group>
          </el-form-item>

          <el-form-item label="图片大小：">
            <div class="flex">
              <el-slider style="width: 70%" v-model="form.width" :min="10" :max="60" />
              <el-input
                type="number"
                style="width: 30%; margin-left: 20px"
                v-model.number="form.width"
                :min="10"
                :max="60"
              >
                <template #append>px</template>
              </el-input>
            </div>
          </el-form-item>

          <el-form-item label="每行数量：">
            <el-radio-group v-model="showComponentData.show_num">
              <el-radio :value="3">3个</el-radio>
              <el-radio :value="4">4个</el-radio>
              <el-radio :value="5">5个</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="文字显示：">
            <el-radio-group v-model="showComponentData.show_name">
              <el-radio label="显示" :value="true" />
              <el-radio label="隐藏" :value="false" />
            </el-radio-group>
          </el-form-item>

          <el-form-item label="文字大小：">
            <div class="flex">
              <el-slider style="width: 70%" v-model="form.fontSize" :min="12" :max="30" />
              <el-input
                type="number"
                style="width: 30%; margin-left: 20px"
                v-model.number="form.fontSize"
                :min="12"
                :max="30"
              >
                <template #append>px</template>
              </el-input>
            </div>
          </el-form-item>

          <el-form-item label="文字颜色：">
            <el-color-picker v-model="form.color"></el-color-picker>
            <el-input style="width: 160px; margin-left: 10px" v-model="form.color">
              <template #append>
                <el-button @click="form.color = '#333333'">重置</el-button>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="文字粗细：">
            <el-radio-group v-model="form.fontWeight">
              <el-radio label="正常" :value="0" />
              <el-radio label="加粗" :value="600" />
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <div class="add-area">
      <p class="add-title">添加按钮</p>
      <VueDraggable
        v-model="showComponentData.default_sub_list"
        :animation="150"
        ghostClass="ghost"
        class="content"
        :filter="'.el-input'"
        :preventOnFilter="false"
        @end="handleUpdateComponent"
      >
        <div
          v-for="(item, index) in showComponentData.default_sub_list"
          :key="item.id"
          class="carousel-item"
        >
          <div class="upload">
            <div class="avatar-uploader" @click="handleChangeImage(index)">
              <el-image
                v-if="item.fileUrl"
                :src="item.fileUrl"
                class="avatar"
                fit="fill"
              ></el-image>
              <i v-else class="el-icon-plus avatar-uploader-icon"
                ><el-icon> <Plus /> </el-icon
              ></i>
              <div v-if="item.fileUrl" class="mask">替换</div>
              <el-icon
                v-if="item.fileUrl"
                class="del-image"
                :size="20"
                @click.stop="handleDelImg(index)"
                ><CircleCloseFilled
              /></el-icon>
            </div>
          </div>
          <div class="operation">
            <div class="column">
              <p class="title">按钮文字</p>
              <el-input v-model="item.fileName" maxlength="5" show-word-limit></el-input>
            </div>
            <div class="column">
              <p class="title">跳转链接</p>
              <div class="link">
                <el-icon style="color: var(--el-color-primary)"><Link /></el-icon>
                <el-link type="primary" @click="handleOpenLinkDialog(index)">{{
                  item.link?.name || '请选择跳转链接'
                }}</el-link></div
              >
            </div>
          </div>

          <el-icon class="del-icon" :size="16" color="red" @click="handleDel(index)"
            ><CircleCloseFilled
          /></el-icon>
        </div>

        <div>
          <!--          <el-button-->
          <!--            type="primary"-->
          <!--            v-if="showComponentData.max_list_count >= showComponentData.default_sub_list.length"-->
          <!--            @click="handleAdd"-->
          <!--            plain-->
          <!--            :icon="Plus"-->
          <!--            style="width: 100%"-->
          <!--            >添加按钮({{ showComponentData.default_sub_list.length }}/{{-->
          <!--              showComponentData.max_list_count-->
          <!--            }})</el-button-->
          <!--          >-->

          <el-tag type="primary" class="submit-btn" @click="handleAdd"
            >添加按钮({{ showComponentData.default_sub_list.length }}/{{
              showComponentData.max_list_count
            }})</el-tag
          >
        </div>
      </VueDraggable>
    </div>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>

    <link-list-dialog
      v-if="showLinkDataDialog"
      :show-link-dialog="showLinkDataDialog"
      @close-dialog="handleCloseLinkDialog"
    ></link-list-dialog>

    <image-selected-dialog
      v-if="showImageChooseDialog"
      :single-type="selectImageSingle"
      :operation-type="false"
      :show-image-choose-dialog="showImageChooseDialog"
      @close-dialog="handleCloseImageDialog"
    ></image-selected-dialog>
  </div>
</template>

<script setup lang="ts">
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'
  import LinkListDialog from '@/views/decoration/diyComponents/LinkListDialog.vue'
  import { VueDraggable } from 'vue-draggable-plus'
  import { Plus, CircleCloseFilled, ArrowRight, ArrowDown, Link } from '@element-plus/icons-vue'
  import debounce from 'lodash/debounce'
  import { reactive, toRaw } from 'vue'
  import ImageSelectedDialog from '@/views/decoration/diyComponents/ImageSelectedDialog.vue'

  const props = defineProps({
    contentData: {
      type: Object,
      default() {
        return {}
      }
    }
  })

  const emit = defineEmits(['updateData'])

  const showComponentData = ref(props.contentData)
  const contentCollapsed = ref(false)
  const showLinkDataDialog = ref(false)
  const showImageChooseDialog = ref(false)
  const selectImageSingle = ref(false)
  const changeIndex = ref()

  interface FormData {
    fontSize: number
    color: string
    fontWeight: number
    borderRadius: number
    width: number
  }

  const form = reactive<FormData>({
    fontSize: 14,
    color: '#333333',
    fontWeight: 0,
    borderRadius: 100,
    width: 30
  })

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)
    showComponentData.value.itemStyle = raw
    handleUpdateComponent()
  }, 300)

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  function handleAdd() {
    if (showComponentData.value.default_sub_list.length > showComponentData.value.max_list_count) {
      return
    }

    showComponentData.value.default_sub_list.push({
      fileName: '新按钮',
      fileUrl: 'https://image.lutongjiakao.com/system/ed20cdb81fe141bc9511bc27eab8927a.png'
    })
  }

  function handleChangeImage(index: number) {
    selectImageSingle.value = true
    changeIndex.value = index
    showImageChooseDialog.value = true
  }

  function handleCloseImageDialog(value: any) {
    if (value) {
      if (Array.isArray(value)) {
        showComponentData.value.default_sub_list.push(...value)
      } else {
        showComponentData.value.default_sub_list[changeIndex.value].fileUrl = value.fileUrl
      }
      handleUpdateComponent()
    }
    showImageChooseDialog.value = false
  }

  function handleCloseLinkDialog(value: any) {
    if (value) {
      showComponentData.value.default_sub_list[changeIndex.value].link = value
    }
    showLinkDataDialog.value = false
  }

  function handleOpenLinkDialog(index: number) {
    showLinkDataDialog.value = true
    changeIndex.value = index
  }

  function handleDelImg(index: number) {
    showComponentData.value.default_sub_list[index].fileUrl = ''
    handleUpdateComponent()
  }

  function handleDel(index: number) {
    showComponentData.value.default_sub_list.splice(index, 1)
    handleUpdateComponent()
  }

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }

  onMounted(() => {
    if (showComponentData.value.itemStyle.fontSize) {
      form.fontSize = showComponentData.value.itemStyle.fontSize
      form.fontWeight = showComponentData.value.itemStyle.fontWeight
      form.borderRadius = showComponentData.value.itemStyle.borderRadius
      form.color = showComponentData.value.itemStyle.color
      form.width = showComponentData.value.itemStyle.width
    }
  })
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      padding-top: 10px;
      padding-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }

    .head-title {
      padding: 20px 0;
      font-size: 14px;
      font-weight: bold;
      color: black;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .add-area {
      width: 100%;
      margin-top: 20px;

      .add-title {
        font-size: 14px;
        font-weight: bold;
        margin-bottom: 20px;
        color: black;
      }

      .content {
        .carousel-item {
          width: 100%;
          margin-bottom: 20px;
          display: flex;
          align-items: center;
          position: relative;
          background-color: #f7f8fa;
          padding: 10px;
          box-sizing: border-box;

          &:hover {
            .del-icon {
              display: block;
            }
          }

          .avatar-uploader {
            width: 70px;
            height: 70px;
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            margin-right: 30px;
            text-align: center;

            .mask {
              position: absolute;
              bottom: 0;
              left: 0;
              width: 100%;
              height: 24px;
              background-color: rgba(0, 0, 0, 0.6);
              display: flex;
              align-items: center;
              justify-content: center;
              opacity: 0;
              transition: opacity 0.3s ease;
              color: #fff;
              font-size: 12px;
            }

            .del-image {
              display: none;
              position: absolute;
              top: 0;
              right: 0;
              cursor: pointer;
              color: #8c939d;
            }

            &:hover .mask {
              opacity: 1;
            }

            &:hover .del-image {
              display: block;
            }
          }

          .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 70px;
            height: 70px;
            line-height: 70px;
            text-align: center;
          }

          .avatar {
            width: 100%;
            height: 70px;
            display: block;
          }

          .del-icon {
            display: none;
            position: absolute;
            top: -2px;
            right: -2px;
            cursor: pointer;
            color: #8c939d;
          }

          .operation {
            flex: 1;

            .column {
              display: flex;
              align-items: center;
              margin-bottom: 6px;
              width: 100%;

              .title {
                width: 80px;
                font-size: 14px;
                margin-right: 20px;
              }

              .link {
                width: 100%;
                display: flex;
                align-items: center;
                height: 40px;
                border-radius: 6px;
                background-color: #fff;
                padding-left: 10px;
              }
            }
          }
        }
      }
    }

    .component-style {
      width: 100%;
      margin-top: 20px;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;

        .title {
          font-size: 14px;
          font-weight: bold;
          color: black;
        }
      }

      .flex {
        display: flex;
        justify-content: space-between;
        width: 100%;
        padding: 0 3px;
      }
    }

    .submit-btn {
      width: 100%;
      height: 40px !important;
      cursor: pointer;
      font-size: 14px;
    }
  }
</style>
